package com.cci.kangdao.dao;

import com.cci.kangdao.dao.model.SSRepairT;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

//授权表
@MapperScan
public interface SSRepairTDao {

    /**
     * 根据服务站id和服务渠道id获取授权表数据
     */
    @Select("select * from SSRepair_T where ServiceStationID = #{ServiceStationID} and CompanyID = #{CompanyID} and Flag = 0")
    List<Map<String, Object>> getSSRepairTByServiceStationIDCompanyID(Map<String, Object> map);

    /**
     * 根据服务站编号和服务渠道id获取授权表数据
     * @param map
     * @return
     */
    @Select("select * from SSRepair_T where BusinessNo = #{stationNo} and CompanyID = #{companyId} and Flag = 0")
    List<Map<String, Object>> getSSRepairTByServiceStationNoCompanyID(Map<String, Object> map);

    /**
     * 根据服务站id获取授权表数据
     */
    @Select("select * from SSRepair_T where ServiceStationID = #{serviceStationID} and Flag = 0")
    List<Map<String, Object>> getSSRepairTByServiceStationID(Long serviceStationID);

    /**
     * 通过服务商Id删除授权记录
     *
     * @param serviceStationID 服务商Id
     * @return 操作行数
     */
    @Update("update SSRepair_T set Flag = -1 where Flag = 0 and ServiceStationID = #{serviceStationID} and CompanyID = #{companyId}")
    int deleteSSRepairTByID(@Param("serviceStationID") Long serviceStationID, @Param("companyId") Long companyId);


    @Select("select stuff((select ',' + b.CompanyNo  from SSRepair_T a\n" +
            "inner join Company_T b on a.CompanyID = b.ID\n" +
            "where a.Flag = 0 and a.ServiceStationID = #{serviceStationID} for xml path('')),1,1,'')  as companyNos")
    String getCompanyNoByServiceStationId(@Param("serviceStationID") long serviceStationID);

    @Select("select stuff((select ',' + b.CompanyName  from SSRepair_T a\n" +
            "            inner join Company_T b on a.CompanyID = b.ID\n" +
            "            where a.Flag = 0 and a.ServiceStationID =5718 for xml path('')),1,1,'')  as companyNos")
    String getCompanyNameByServiceStationId(@Param("serviceStationID") long serviceStationID);


    @Select("select count(distinct CompanyID) from SSRepair_T where Flag = 0 and ServiceStationID = #{serviceStationID}")
    int getSsrepairCount(@Param("serviceStationID") long serviceStationID);

    @Select("select * from SSRepair_T where CompanyID = #{companyID} and ServiceStationID = #{serviceStationID} AND Flag = 0")
    Map<String, Object> selectSsRepairTByCompanyIDAndServiceStationID(@Param("companyID") long companyID, @Param("serviceStationID") Long serviceStationID);

    /**
     * 根据服务站id和服务渠道id获取授权表数据
     */
    @Select({"<script>" +
            "select  *  from SSRepair_T where BusinessNo in " +
            "<foreach item = 'item' index = 'index' collection = 'BusinessNos' open = '(' separator = ',' close = ')'>#{item}</foreach> and companyID= 1 AND Flag = 0" +
            "</script>"}
    )
    List<SSRepairT> getSSRepairTByBusinessNos(@Param("BusinessNos") List<String> BusinessNos);

     /**
      * @description:根据条件查询授权表是否有数据
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2022/5/19 15:43
      * @version: V1.0
      */
     @Select("select * from SSRepair_T where ServiceStationID = #{ServiceStationID} and CompanyID = #{CompanyID} and BusinessNo = #{stationNo}  and Flag = 0")
     List<SSRepairT> queryAuthByStationId(@Param("stationId") Long stationId, @Param("companyId") Long companyId, @Param("stationNo") String stationNo);

    @Select("select * from SSRepair_T where CompanyID = #{CompanyID} and BusinessNo = #{stationNo}  and Flag = 0")
    List<SSRepairT> queryAuthByStationNo(@Param("CompanyID") Long CompanyID, @Param("stationNo") String stationNo);

    @Select("select top 1 CompanyID from SSRepair_T where BusinessNo = #{businessNo}  and Flag = 0")
    Long getCompanyIdBybusinessNo(@Param("businessNo") String businessNo);

    @Update("update SSRepair_T set Flag = -1 where Flag = 0 and BusinessNo = #{stationNo} and CompanyID = #{companyId}")
    int deleteSSRepairTByStationNo(@Param("stationNo") String stationNo, @Param("companyId") Long companyId);


//    @Update("update SSRepair_T set isTechSupport = #{isTechSupport} where Flag = 0 and id = #{id}")
//    int updateIsTechSupport(SSRepairT ssRepairT);

//    /**
//     * 将全部的大工匠工服务站清除
//     *
//     * @param companyID
//     * @return
//     */
//    @Update("update SSRepair_T set isTechSupport = null where Flag = 0 and CompanyID = #{companyID}")
//    int updateNullIsTechSupport(Long companyID);

    @Select("SELECT * FROM SSRepair_T ss " +
            "INNER JOIN Company_T company ON ss.CompanyID = company.ID AND company.Flag = 0 AND ss.Flag = 0 " +
            "WHERE company.CompanyNo = #{CompanyNo} AND ss.ServiceStationID = #{serviceStationID}")
    public SSRepairT getSSRepairTByCompanyNoAndServiceStationID(@Param("CompanyNo") String CompanyNo, @Param("serviceStationID") Long serviceStationID);

    /**
     * 根据二网服务站id，查找某渠道的一网的授权信息
     * @param CompanyNo 渠道no
     * @param serviceStationID 二网服务站id
     * @return
     */
    @Select("SELECT top 1 ss.* FROM SSRepair_T ss " +
            "INNER JOIN Company_T company ON ss.CompanyID = company.ID AND company.Flag = 0 AND ss.Flag = 0 " +
            "INNER JOIN ServiceStationRelation_T ssr ON ss.ServiceStationID = ssr.ParentID AND ssr.Flag = 0 " +
            "WHERE company.CompanyNo = #{CompanyNo} AND ssr.ChildID = #{serviceStationID} ")
    public SSRepairT getSSRepairTByCompanyNoAndChildServiceStationID( @Param("CompanyNo") String CompanyNo,@Param("serviceStationID") Long serviceStationID);
    
    /**
     * 根据二网服务站授权号，查找该渠道的一网的授权信息
     * @param CompanyNo 渠道no
     * @param serviceStationID 二网服务站id
     * @return
     */
    @Select("SELECT top 1 ss.* FROM SSRepair_T ss " +
            "INNER JOIN Company_T company ON ss.CompanyID = company.ID AND company.Flag = 0 AND ss.Flag = 0 " +
            "INNER JOIN ServiceStationRelation_T ssr ON ss.ServiceStationID = ssr.ParentID AND ssr.Flag = 0 " +
            "INNER JOIN SSRepair_T ss2 ON ss2.ServiceStationID = ssr.ChildID AND ssr.Flag = 0 and ss2.Flag = 0 and ss2.CompanyID = company.ID " +
            "WHERE company.CompanyNo = #{CompanyNo} AND ss2.BusinessNo = #{BusinessNo} ")
    public SSRepairT getSSRepairTByCompanyNoAndChildBusinessNo( @Param("CompanyNo") String CompanyNo,@Param("BusinessNo") String BusinessNo);
}
